package com.e2oto.seller.widget;

/**
 * Created by ZhangLiQiang on 2017/9/15.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.e2oto.seller.R;


/**
 * Created by IT_ZJYANG on 2016/9/9.
 * 标题栏
 */
public class CommonTitle extends LinearLayout {

    private Boolean isLeftBtnVisible;
    private int leftResId;

    private Boolean isLeftTvVisible;
    private String leftTvText;

    private Boolean isRightBtnVisible;
    private int rightResId;

    private Boolean isRightTvVisible;
    private String rightTvText;

    private Boolean isTitleVisible;
    private String titleText;
    private int titleTextColor;

    private int backgroundResId;

    public CommonTitle(Context context) {
        this(context, null);
    }

    public CommonTitle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    /**
     * 初始化属性
     *
     * @param attrs
     */
    public void initView(AttributeSet attrs) {

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CommonTitle);
        /**-------------获取左边按钮属性------------*/
        isLeftBtnVisible = typedArray.getBoolean(R.styleable.CommonTitle_left_btn_visible, false);
        leftResId = typedArray.getResourceId(R.styleable.CommonTitle_left_btn_src, -1);
        /**-------------获取左边文本属性------------*/
        isLeftTvVisible = typedArray.getBoolean(R.styleable.CommonTitle_left_tv_visible, false);
        if (typedArray.hasValue(R.styleable.CommonTitle_left_tv_text)) {
            leftTvText = typedArray.getString(R.styleable.CommonTitle_left_tv_text);
        }
        /**-------------获取右边按钮属性------------*/
        isRightBtnVisible = typedArray.getBoolean(R.styleable.CommonTitle_right_btn_visible, false);
        rightResId = typedArray.getResourceId(R.styleable.CommonTitle_right_btn_src, -1);
        /**-------------获取右边文本属性------------*/
        isRightTvVisible = typedArray.getBoolean(R.styleable.CommonTitle_right_tv_visible, false);
        if (typedArray.hasValue(R.styleable.CommonTitle_right_tv_text)) {
            rightTvText = typedArray.getString(R.styleable.CommonTitle_right_tv_text);
        }
        /**-------------获取标题属性------------*/
        isTitleVisible = typedArray.getBoolean(R.styleable.CommonTitle_title_visible, false);
        if (typedArray.hasValue(R.styleable.CommonTitle_title_text)) {
            titleText = typedArray.getString(R.styleable.CommonTitle_title_text);
        }
        if (typedArray.hasValue(R.styleable.CommonTitle_title_text_color)) {
            titleTextColor =typedArray.getColor(R.styleable.CommonTitle_title_text_color, 0XFFFFFFFF);

        }

        /**-------------背景颜色------------*/
        backgroundResId = typedArray.getResourceId(R.styleable.CommonTitle_barBackground, -1);

        typedArray.recycle();

        /**-------------设置内容------------*/
        View barLayoutView = View.inflate(getContext(), R.layout.layout_common_toolbar, null);
        ImageView leftBtn = (ImageView) barLayoutView.findViewById(R.id.toolbar_left_btn);
        TextView leftTv = (TextView) barLayoutView.findViewById(R.id.toolbar_left_tv);
        TextView titleTv = (TextView) barLayoutView.findViewById(R.id.toolbar_title_tv);
        ImageView rightBtn = (ImageView) barLayoutView.findViewById(R.id.toolbar_right_btn);
        TextView rightTv = (TextView) barLayoutView.findViewById(R.id.toolbar_right_tv);
        RelativeLayout barRlyt = (RelativeLayout) barLayoutView.findViewById(R.id.toolbar_content_rlyt);

        if (isLeftBtnVisible) {
            leftBtn.setVisibility(VISIBLE);
        }
        if (isLeftTvVisible) {
            leftTv.setVisibility(VISIBLE);
        }
        if (isRightBtnVisible) {
            rightBtn.setVisibility(VISIBLE);
        }
        if (isRightTvVisible) {
            rightTv.setVisibility(VISIBLE);
        }
        if (isTitleVisible) {
            titleTv.setVisibility(VISIBLE);
        }
        leftTv.setText(leftTvText);
        rightTv.setText(rightTvText);
        titleTv.setText(titleText);
        titleTv.setTextColor(titleTextColor);

        if (leftResId != -1) {
            leftBtn.setImageResource(leftResId);
        }
        if (rightResId != -1) {
            rightBtn.setImageResource(rightResId);
        }
        if (backgroundResId != -1) {
            barRlyt.setBackgroundColor(backgroundResId);
        }
        //将设置完成之后的View添加到此LinearLayout中
        addView(barLayoutView, 0);
    }
}